import router from './router'
import store from './store'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
let whiteList = ['/login', '/404']
router.beforeEach(async (to, from, next) => {
  nprogress.start()
  /* must call `next` */
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if(!store.getters.userId){
        // 在这里获取用户信息
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  nprogress.done()
})
